﻿using Kz.Telegram.Bot.Console.HandleUpdate.MessageTypeHandler;
using Kz.Telegram.Bot.Infrastructure.Extensions;
using SqlSugar;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using static System.Net.Mime.MediaTypeNames;

namespace Kz.Telegram.Bot.Console.HandleUpdate
{
    public class MessageHandler : IHandler
    {
        public async Task Handle(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
        {
            var msg = update.Message!;
            var typ = msg.Chat.Type!;   //判断是从群组内发出，还是从机器人单聊中发出

            if (msg.From?.IsBot == true) //机器人消息
            {
                if (msg.From.Username == "GroupAnonymousBot")
                {
                    System.Console.WriteLine($"群系统消息:{msg.Text}");
                    return;
                }
            }

            IHandler handler = default!;

            switch (msg.Type)
            {
                //未知
                case MessageType.Unknown:
                    break;
                //文字
                case MessageType.Text:
                    handler = new MessageTextHandler();
                    break;
                //照片
                case MessageType.Photo:
                    {

                        foreach (var p in msg.Photo!)
                        {
                            using FileStream fs = new($"{SnowFlakeSingle.Instance.NextId()}.png", FileMode.Create);
                            ////获取文件信息 & 下载文件
                            //{
                            //    var file = await botClient.GetFileAsync(p.FileId, cancellationToken);
                            //    await botClient.DownloadFileAsync(file.FileId, fs, cancellationToken);
                            //}
                            //获取文件信息同时下载
                            {
                                await botClient.GetInfoAndDownloadFile(p.FileId, fs, cancellationToken);
                            }
                        }
                    }
                    break;
                //音频
                case MessageType.Audio:
                    {
                        var ext = Path.GetExtension(msg.Audio!.FileName);
                        using FileStream fs = new($"{SnowFlakeSingle.Instance.NextId()}{ext}", FileMode.Create);
                        ////获取文件信息 & 下载文件
                        //{
                        //    var file = await botClient.GetFileAsync(msg.Audio!.FileId, cancellationToken);
                        //    await botClient.DownloadFileAsync(file.FilePath!, fs, cancellationToken);
                        //}
                        //获取文件信息同时下载
                        {
                            await botClient.GetInfoAndDownloadFile(msg.Audio!.FileId, fs, cancellationToken);
                        }
                    }
                    break;
                //视频
                case MessageType.Video:
                    {
                        var ext = Path.GetExtension(msg.Video!.FileName);
                        using FileStream fs = new($"{SnowFlakeSingle.Instance.NextId()}{ext}", FileMode.Create);
                        ////获取文件信息 & 下载文件
                        //{
                        //    var file = await botClient.GetFileAsync(msg.Video!.FileId, cancellationToken);
                        //    await botClient.DownloadFileAsync(file.FilePath!, fs, cancellationToken);
                        //}
                        //获取文件信息同时下载
                        {
                            await botClient.GetInfoAndDownloadFile(msg.Video!.FileId, fs, cancellationToken);
                        }
                    }
                    break;
                //语音
                case MessageType.Voice:
                    {
                        using FileStream fs = new($"{SnowFlakeSingle.Instance.NextId()}.wav", FileMode.Create);
                        ////获取文件信息 & 下载文件
                        //{
                        //    var file = await botClient.GetFileAsync(msg.Voice!.FileId, cancellationToken);
                        //    await botClient.DownloadFileAsync(file.FilePath!, fs, cancellationToken);
                        //}
                        //获取文件信息同时下载
                        {
                            await botClient.GetInfoAndDownloadFile(msg.Voice!.FileId, fs, cancellationToken);
                        }
                    }
                    break;
                //文档|文件
                case MessageType.Document:
                    {
                        var ext = Path.GetExtension(msg.Document!.FileName);
                        using FileStream fs = new($"{SnowFlakeSingle.Instance.NextId()}{ext}", FileMode.Create);
                        ////获取文件信息 & 下载文件
                        //{
                        //    var file = await botClient.GetFileAsync(msg.Document!.FileId, cancellationToken);
                        //    await botClient.DownloadFileAsync(file.FilePath!, fs, cancellationToken);
                        //}
                        //获取文件信息同时下载
                        {
                            await botClient.GetInfoAndDownloadFile(msg.Document!.FileId, fs, cancellationToken);
                        }
                    }
                    break;
                //表情
                case MessageType.Sticker:
                    break;
                //位置
                case MessageType.Location:
                    {
                        //msg.Location
                    }
                    break;
                //联系人
                case MessageType.Contact:
                    {
                        //msg.Contact
                    }
                    break;
                //地点
                case MessageType.Venue:
                    {
                        //msg.Venue.Location.Latitude
                        //msg.Venue.Location.Longitude
                        //msg.Venue.Title;
                        //msg.Venue.Address;

                    }
                    break;
                //游戏
                case MessageType.Game:
                    break;
                //视频文档
                case MessageType.VideoNote:
                    break;
                //发票
                case MessageType.Invoice:
                    break;
                //付款成功提醒
                case MessageType.SuccessfulPayment:
                    break;
                ////网站成功连接
                //case MessageType.WebsiteConnected:
                //    break;
                ////用户进群
                //case MessageType.ChatMembersAdded:
                //    break;
                ////用户|机器人退群
                //case MessageType.ChatMemberLeft:
                //    {
                //        //msg.LeftChatMember
                //        //msg.LeftChatMember.IsBot
                //    }
                //    break;
                ////群标题变更
                //case MessageType.ChatTitleChanged:
                //    {
                //        //msg.NewChatTitle;
                //    }
                //    break;
                ////群头像变更
                //case MessageType.ChatPhotoChanged:
                //    {
                //        //msg.NewChatPhoto
                //    }
                //    break;
                ////消息被置顶
                //case MessageType.MessagePinned:
                //    {
                //        //msg.PinnedMessage
                //    }
                //    break;
                ////群头像被删除
                //case MessageType.ChatPhotoDeleted:
                //    {
                //        //msg.DeleteChatPhoto
                //    }
                //    break;
                ////群被创建
                //case MessageType.GroupCreated:
                //    break;
                ////超级群被创建
                //case MessageType.SupergroupCreated:
                //    break;
                ////频道被创建
                //case MessageType.ChannelCreated:
                //    break;
                //case MessageType.MigratedToSupergroup:
                //    break;
                //case MessageType.MigratedFromGroup:
                //    break;
                //投票
                case MessageType.Poll:
                    {
                        //msg.Poll
                    }
                    break;
                case MessageType.Dice:
                    {
                        //msg.Dice
                    }
                    break;
                //消息自动销毁时间变更
                case MessageType.MessageAutoDeleteTimerChanged:
                    break;
                case MessageType.ProximityAlertTriggered:
                    break;
                //小程序数据
                case MessageType.WebAppData:
                    {

                    }
                    break;
                case MessageType.VideoChatScheduled:
                    break;
                case MessageType.VideoChatStarted:
                    break;
                case MessageType.VideoChatEnded:
                    break;
                case MessageType.VideoChatParticipantsInvited:
                    break;
                //动画/GIF图
                case MessageType.Animation:
                    {
                        //msg.Animation.FileId
                    }
                    break;
                case MessageType.ForumTopicCreated:
                    break;
                case MessageType.ForumTopicClosed:
                    break;
                case MessageType.ForumTopicReopened:
                    break;
                case MessageType.ForumTopicEdited:
                    break;
                case MessageType.GeneralForumTopicHidden:
                    break;
                case MessageType.GeneralForumTopicUnhidden:
                    break;
                case MessageType.WriteAccessAllowed:
                    break;
                //用户分享了机器人
                case MessageType.UsersShared:
                    {
                        //msg.UserShared
                    }
                    break;
                //聊天分享了机器人
                case MessageType.ChatShared:
                    {
                        //msg.ChatShared
                    }
                    break;
                default:
                    break;
            }

            if (handler != default)
            {
                await handler.Handle(botClient, update, cancellationToken);
            }
        }
    }
}